import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.nio.charset.Charset;

public class WebService {
    public void test() throws IOException {

        StringBuffer stringBuffer = new StringBuffer("");
        stringBuffer.append("<ns:GET_Input " +
                "xmlns:ns=\"http://ebssit.streamax.com:8006/webservices/rest/common_service/INVOKEFMSWS/\"" +
                "xmlns:ns1=\"http://xmlns.oracle.com/apps/fnd/soaprovider/plsql/rest/common_service/header\">" +
                "<ns:InputParameters>" +
                "<ns:P_IFACE_CODE>CUX_WIP_TRANS_I</ns:P_IFACE_CODE>" +
                "<ns:P_BATCH_NUMBER>201804160001</ns:P_BATCH_NUMBER>" +
                "<ns:P_REQUEST_DATA>PHJvb3Q+PGhlYWQ+PEg+CjxRVUVVRV9JRD4xMzU3MDk2NTQzPC9RVUVVRV9JRD4KPFRSWF9EQVRFPjIwMjAwNDI3MDAwMjwvVFJYX0RBVEU+CjxTT1VSQ0VfTlVNPjEyMzQ1Njc4OTwvU09VUkNFX05VTT4KPFNSX1RZUEU+Q29tcGxldGlvbjwvU1JfVFlQRT4KPE9SR19JRD4xMDQ8L09SR19JRD4KPFNUT1JFX0lEPktKXzAzMDU8L1NUT1JFX0lEPgo8RVJQX01PPktIMTkxMTAwMDY8L0VSUF9NTz4KPEVSUF9JRD43MjkwODwvRVJQX0lEPgo8TVRSTF9JRD41MDY3NDc5MDwvTVRSTF9JRD4KPE1UUkxfTk8+U01EWi44MC0wMTEwLTA1LUxGQzwvTVRSTF9OTz4KPFFVQU5USVRZPjEwPC9RVUFOVElUWT4KPFJFTUFSSz4xMDwvUkVNQVJLPgo8TE9UX05PPktIMTkxMTAwMDY8L0xPVF9OTz4KPE1VTklUPlBDUzwvTVVOSVQ+CjxPUEVSQVRJT05fU0VRX05VTT4xMDwvT1BFUkFUSU9OX1NFUV9OVU0+CjxDcmVhdGlvbkVtcE5vPjwvQ3JlYXRpb25FbXBObz4KPC9IPjwvaGVhZD48L3Jvb3Q+CiA=</ns:P_REQUEST_DATA> " +
                "</ns:InputParameters>" +
                "</ns:GET_Input>");
        String requsetDataMessage = String.valueOf(stringBuffer);
        String userName = "ASADMIN";
        String password = "asadmin";
        String postUrl = "http://ebssit.streamax.com:8006/webservices/rest/INVOKEFMSWS/INVOKEFMSWS/";
        String host = "http://ebssit.streamax.com";
        int port = 8006 ;
        CredentialsProvider provider =  new BasicCredentialsProvider();
        provider.setCredentials(new AuthScope(host, port), new UsernamePasswordCredentials(userName, password));

        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
        CloseableHttpResponse response = null;
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000000).setConnectTimeout(5000000).build();

        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(provider);

        HttpPost httpPost = new HttpPost(postUrl);
        httpPost.setConfig(requestConfig);
        httpPost.setHeader("Content-Type","application/xml");

        StringEntity data = new StringEntity(requsetDataMessage, Charset.forName("UTF-8"));
        data.setContentType("application/xml");
        httpPost.setEntity(data);

        response = httpClient.execute(httpPost, context);
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println( "StatusCode : " + statusCode);
        if ( statusCode == 200) {
            System.out.println("response : " + response);
        }else {
            System.out.println("访问错误 : " + response);
        }
    }

    public void test2() throws IOException {
        String wadl = "http://ebssit.streamax.com:8006/webservices/rest/INVOKEFMSWS/INVOKEFMSWS/";
        StringBuffer stringBuffer = new StringBuffer("");
        stringBuffer.append("<ns:GET_Input" +
                "xmlns:ns=\"http://ebssit.streamax.com:8006/webservices/rest/common_service/INVOKEFMSWS/\"" +
                "xmlns:ns1=\"http://xmlns.oracle.com/apps/fnd/soaprovider/plsql/rest/common_service/header\">" +
                "<ns:InputParameters>" +
                "<ns:P_IFACE_CODE>CUX_WIP_TRANS_I</ns:P_IFACE_CODE>" +
                "<ns:P_BATCH_NUMBER>201804160001</ns:P_BATCH_NUMBER>" +
                "<ns:P_REQUEST_DATA>PHJvb3Q+PGhlYWQ+PEg+CjxRVUVVRV9JRD4xMzU3MDk2NTQzPC9RVUVVRV9JRD4KPFRSWF9EQVRFPjIwMjAwNDI3MDAwMjwvVFJYX0RBVEU+CjxTT1VSQ0VfTlVNPjEyMzQ1Njc4OTwvU09VUkNFX05VTT4KPFNSX1RZUEU+Q29tcGxldGlvbjwvU1JfVFlQRT4KPE9SR19JRD4xMDQ8L09SR19JRD4KPFNUT1JFX0lEPktKXzAzMDU8L1NUT1JFX0lEPgo8RVJQX01PPktIMTkxMTAwMDY8L0VSUF9NTz4KPEVSUF9JRD43MjkwODwvRVJQX0lEPgo8TVRSTF9JRD41MDY3NDc5MDwvTVRSTF9JRD4KPE1UUkxfTk8+U01EWi44MC0wMTEwLTA1LUxGQzwvTVRSTF9OTz4KPFFVQU5USVRZPjEwPC9RVUFOVElUWT4KPFJFTUFSSz4xMDwvUkVNQVJLPgo8TE9UX05PPktIMTkxMTAwMDY8L0xPVF9OTz4KPE1VTklUPlBDUzwvTVVOSVQ+CjxPUEVSQVRJT05fU0VRX05VTT4xMDwvT1BFUkFUSU9OX1NFUV9OVU0+CjxDcmVhdGlvbkVtcE5vPjwvQ3JlYXRpb25FbXBObz4KPC9IPjwvaGVhZD48L3Jvb3Q+CiA=</ns:P_REQUEST_DATA> \n" +
                "</ns:InputParameters>" +
                "</ns:GET_Input>");

        HttpPost httpPost = new HttpPost(wadl);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000000).setConnectTimeout(5000000).build();
        StringEntity stringEntity = new StringEntity(String.valueOf(stringBuffer), Charset.forName("UTF-8"));

        String userName = "ASADMIN";
        String password = "asadmin";
        String host = "http://ebssit.streamax.com";
        int port = 8006;
        CredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(new AuthScope(host, port), new UsernamePasswordCredentials(userName, password));

        HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();

        httpPost.setConfig(requestConfig);
        httpPost.setHeader("Content-Type", "application/xml");
        httpPost.setEntity(stringEntity);

        HttpClientContext context = HttpClientContext.create();
        CloseableHttpResponse closeableHttpResponse = (org.apache.http.client.methods.CloseableHttpResponse) httpClient.execute(httpPost, context);
        System.out.println(closeableHttpResponse);
    }

}